<?php
namespace Home\Controller;

use Think\Controller;

/**
*@desc 普通用户后台
*/
class UserReportOrderController extends CommonController {
    private $auth_status = 0;//默认：未认证
    private $user_email = null;//用户邮箱
    private $uid = null;
    private $user_token = null;
    private $apis_table = "dmp_service_apis_image";//api信息 镜像表
    private $auth_type = 1;

    public function __construct(){
        parent::__construct();
        //是否普通用户，且是登录状态
        
        $this->getAuthStatus();
        $this->auth_status = session('37degree_auth_status');
        
        $this->user_email = session('user_email');
        $this->assign('user_email',$this->user_email);
        $this->uid = session('uid');
        $this->user_token = session('user_token');
        $this->auth_type = session('auth_type');

        $this->assign('user_token',$this->user_token);

        if(empty($this->uid)) redirect('/member/login.html');

    }

    //我的订单
    public function index(){
        $this->assign('head_title',"我的订单－云途数据市场");
        $this->display();
    }

    //ajax获取订单列表
    public function ajaxOrderList(){
        $page = I('post.page',1,'intval');
        $pagesize = I('post.pagesize',20,'intval');
        $type = I('post.type','all');

        switch($type){
            case 'pendingpay':
                $pay_status = array(0,-1);
                break;
            case 'alreadypay':
                $pay_status = array(1,2);
                break;
            default:
                $pay_status = null;
                break;
        }
        //是否登录
        if(empty($this->uid)){
            $this->ajaxReturn( array('status'=>4001,'msg'=>'请登录','data'=>null) );
        }
        $orderModel = D('ReportOrders');
        $list = $orderModel->getListWithUser($this->uid,$pay_status,$page,$pagesize);
        // timp($list);die;
        
        $this->ajaxReturn( array('status'=>200,'msg'=>'OK','data'=>$list) );
    }

    //取消订单（已经取消的订单不需要重新取消，已经付款成功的不能取消）
    public function ajaxCancelOrder(){
        $order_id = I('post.order_id',null);
        if(empty($order_id)){
            $this->ajaxReturn( array('status'=>4001,'msg'=>'请登录','data'=>null) );
        }

        if(empty($order_id)){
            $this->ajaxReturn( array('status'=>4005,'msg'=>'数据缺失','data'=>null) );   
        }

        $orderModel = D('ReportOrders');
        $info = $orderModel->getInfo($order_id);
        
        if(empty($info)){
            $this->ajaxReturn( array('status'=>4006,'msg'=>'改订单不存在','data'=>null) );      
        }
        //是否给用户订单
        if($this->uid != $info['uid']){
            $this->ajaxReturn( array('status'=>4007,'msg'=>'您无权取消该订单','data'=>null) );         
        }

        if($info['enable']==0){
            $this->ajaxReturn( array('status'=>4008,'msg'=>'该订单已经取消','data'=>null) );            
        }

        $result = $orderModel->chanelOrder($order_id);
        if($result){
            $this->ajaxReturn( array('status'=>200,'msg'=>'订单取消成功','data'=>null) );
        }else{
            $this->ajaxReturn( array('status'=>4009,'msg'=>'订单取消失败','data'=>null) );
        }
    }
    

}
